
<!--{include file="public/top" /}-->

<body class="layui-anim layui-anim-up">

<div class="x-body">
    <div class="layui-row clear pagesa">
        <p class="fl">>单页列表</p><br><br><span>此模块的内容请不要随意删除，如要删除请联系标梵互动技术人员。</span>
    </div>
    <div class="layui-row" style="margin: 10px 0 -10px 0">
        <form class="layui-form layui-col-md12 x-so">
            <input style="width: 200px" type="text" name="name"  placeholder="请输入名称" autocomplete="off" class="layui-input">
            <button class="layui-btn" type="button"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
            <!--分开-->
            <button class="layui-btn layui-btn-danger fr" id="del" type="button"><i class="layui-icon ">&#xe640;</i>批量删除</button>
            <button class="layui-btn layui-btn-warm  fr" id="refresh" type="button"><i class="layui-icon ">&#xe63f;</i>刷新</button>
            <button class="layui-btn layui-btn-normal fr" id="add" type="button"><i class="layui-icon ">&#xe654;</i>添加</button>
        </form>
    </div>
    <table class="" lay-filter="test" id="table" >
    </table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn  layui-btn-xs" lay-event="edit1">预览</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit2">是否上架</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
</div>
<script>
    //接口地址
    //获取列表接口
    var listurl = '/admin/Page/getList';
    //添加页面接口
    var addurl = "/admin/Page/add";
    //修改页面接口
    var upurl = '/admin/Page/up/id/Id';
    //删除接口
    var delurl = '/admin/Page/del';
    //批量删除接口
    var delAllUrl = '/admin/Page/delAll';
    //获取一条数据接口
    var getOneurl = '/admin/Page/getOne/id/';
    //排序接口
    var sortUrl = '/admin/Page/sort';
    //上架
    var auditurl = '/admin/Page/shangjia';
    layui.use('table', function(){
        var table = layui.table;
        //初始化表格
        table.render({
            elem: '#table'
            ,height: 'full-140'
            ,url: listurl //数据接口
            ,page: true //开启分页
            ,limit:20
            ,limits:[20,100,300]
            ,id:'idTest'
            ,cols: [[ //表头
                {field: 'id',width:40,title: 'ID', fixed: 'left',style:'height: 50px;'}
                ,{type: 'checkbox'}
                ,{field: 'lanmu', title: '归属栏目'}
                ,{field: 'name', title: '单页名称'}
                ,{field: 'title', title: '标题(SEO)'}
                ,{field: 'keyword', title: '关键词(SEO)'}
                ,{field: 'description', title: '描述(SEO)'}
                ,{field: 'create_time', title: '创建时间'}
                ,{field: 'flag', title: '是否上架'}
                ,{field: 'sort',fixed: 'right', title:'排序' ,width:70,style:'cursor: pointer;height:50px', event: 'sort',sort: true}
                ,{fixed: 'right',width:230, align:'center', toolbar: '#barDemo'}
            ]]
        });
        //监听表格复选框选择
        //监听工具条
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //监听删除
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: delurl,
                        data: {'id':data.id},
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                layer.msg(success.msg);
                                obj.del();
                                layer.close(index);
                            }else {
                                layer.msg(success.msg)
                            }

                        }
                    });

                });
            }

            if(obj.event === 'sort'){

                layer.prompt({
                    formType: 2
                    ,title: '修改 ID 为 ['+ data.id +'] 的排序'
                    ,value: data.sort
                }, function(value, index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: sortUrl,
                        data:{
                            'id':data.id,
                            'sort':value
                        },
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                //同步更新表格和缓存对应的值
                                obj.update({
                                    sort: value
                                });
                                layer.msg(success.msg, {
                                    time: 1000
                                });
                            }else {
                                layer.msg(success.msg);
                            }
                        }
                    });

                });
            }
            //监听预览
            if(obj.event === 'edit1'){
                var url1 = '';
                if(data.id === 7){
                     url1 = '/About-us.html';
                }else if(data.id===8){
                    url1 = '/After-Sale-Services.html';
                }else if(data.id===10){
                    url1 = '/Contact-us.html';
                }else if(data.id===11){
                    url1 = '/Certification.html';
                }else if(data.id===12){
                    url1 = '/Manufacturing-Capacity.html';
                }else if(data.id===13){
                    url1 = '/Workshop.html';
                }else if(data.id===14){
                    url1 = '/Warehouse.html';
                }else if(data.id===15){
                    url1 = '/Customer-Reviews.html';
                }else if(data.id===16){
                    url1 = '/Private-Policy.html';
                }

                window.open(url1);
            }
            //监听上架
            if(obj.event === 'edit2'){
                layer.confirm('请选择是否上架', {
                    btn: ['是','否'] //按钮
                }, function(){
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: auditurl,
                        data: {'id':data.id,'flag':1},
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                obj.update({
                                    flag: '已上架'
                                });
                                layer.msg(success.msg);
                                layer.close(index);

                            }else {
                                layer.msg(success.msg)
                            }

                        }
                    });
                }, function(){
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: auditurl,
                        data: {'id':data.id,'flag':2},
                        success: function (res) {
                            var success = JSON.parse(res);
                            if(success.code===200){
                                obj.update({
                                    flag: '未上架'
                                });
                                layer.msg(success.msg);
                                layer.close(index);

                            }else {
                                layer.msg(success.msg)
                            }

                        }
                    });
                });

            }
            //监听修改
            if(obj.event === 'edit'){
                var url = upurl;
                url = url.replace('Id',data.id);
                layer.open({
                    type: 2,
                    area: ["1200px","800px"],
                    fix: false, //不固定
                    maxmin: true,
                    shade:0.4,
                    title: '修改',
                    content:url ,
                    end: function(){
                        $.post(getOneurl+data.id,function(res) {
                            var success = JSON.parse(res);
                            obj.update({
                                name: success.data.name,
                                title: success.data.title,
                                keyword: success.data.keyword,
                                description: success.data.description
                            });
                        });

                    }
                });
            }
        });
        //添加
        $('#add').click(function () {
            layer.open({
                type: 2,
                area:  ["1200px","800px"],
                fix: false, //不固定
                maxmin: true,
                shade:0.4,
                title: '添加',
                content: addurl,
                end: function(){
                    table.reload('idTest', {
                        url: listurl
                        ,where: {'name':0} //设定异步数据接口的额外参数
                        //,height: 300
                    });

                }
            });
        });
        //批量删除
        $('#del').click(function () {
            var checkStatus = table.checkStatus('idTest');
            if(checkStatus.data.length===0){
                layer.msg("请选择要删除的行");
                return false;
            }
            layer.confirm('真的删除行么', function(index){
                var data1 = checkStatus.data;
                var data2 = [];
                for (var i=0;i<data1.length;i++)
                {
                    data2[i] = data1[i].id;
                }
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: delAllUrl,
                    data: {data:data2},
                    success: function (res) {
                        var success = JSON.parse(res);
                        if(success.code==200){
                            layer.msg(success.msg);
                            table.reload('idTest', {
                                url: listurl
                                ,where: {'name':0} //设定异步数据接口的额外参数
                                //,height: 300
                            });
                        }else {
                            layer.msg(success.msg)
                        }

                    }
                });
            });
        });
        //刷新表格
        $('#refresh').click(function () {
            table.reload('idTest', {
                url: listurl
                ,where: {'name':0} //设定异步数据接口的额外参数
                //,height: 300
            });
        });
        //监听搜索提交
        var form = layui.form,layer = layui.layer;
        form.on('submit(sreach)', function(data){
            table.reload('idTest', {
                url: listurl
                ,where: data.field //设定异步数据接口的额外参数
                //,height: 300
            });
        });


    });




</script>

</body>

</html>
